package com.ybg.common.web;

import com.ybg.common.redis.RedisDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.format.datetime.DateFormatter;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
@ComponentScan("com.ybg.common.redis")
public class WebMvcConfig implements WebMvcConfigurer {
//    @Value("${sa-token-keys.user-key}")
//    private String userKey;
//    @Value("${sa-token-keys.role-key}")
//    private String roleKey;
//    @Value("${sa-token-keys.permission-key}")
//    private String permissionKey;
    private RedisDao redisDao;

    @Autowired
    public WebMvcConfig(RedisDao redisDao) {
        this.redisDao = redisDao;
    }

    @Override
    public void addFormatters(FormatterRegistry registry) {
        registry.addFormatter(new DateFormatter("yyyy-MM-dd"));
        registry.addFormatter(new DateFormatter("yyyy-MM-dd-HH:mm:ss"));
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("PUT","GET","POST")
                .allowedHeaders("tcm-snd-token")
                .allowCredentials(false)
                .maxAge(300);
    }

//    @Override
//    public void addInterceptors(InterceptorRegistry registry) {
//        // @saCheck(login|Role|Permission)(value = "admin")
//        // Controller => Method
//        registry.addInterceptor(new SaInterceptor(handler->{
//            SaRouter.match("/user/*","/user/login",r->StpUtil.checkLogin());
//            SaRouter.match("/admin/user",r->StpUtil.hasRole("admin"));
//        }));
//    }
//
//    @Bean
//    public StpInterface stpInterface(){
//        return new StpInterface(){
//            @Override
//            public List<String> getPermissionList(Object userId, String type) {
//                return redisDao.getHash(permissionKey,userId.toString());
//            }
//
//            @Override
//            public List<String> getRoleList(Object userId, String type) {
//                return redisDao.getHash(roleKey,userId.toString());
//            }
//        };
//    }
//
//    @Bean
//    public StpLogic stpLogic(){
//        return new StpLogicJwtForStateless();
//    }
}
